package net.hhk.controller;

import java.io.InputStream;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;

import net.hhk.controller.lazyload.ImageLoader;
import net.hhk.data.Tweet;
import net.hhk.twitter.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TweetAdapter extends BaseAdapter {
	private List<Tweet> socialFeed;
	private Context context;
	private ImageLoader imageLoader;
	private static LayoutInflater inflater = null;
	private Activity activity;

	public TweetAdapter(Activity context, List<Tweet> socialFeed) {
		super();
		this.socialFeed = socialFeed;
		this.context = context;
		this.activity = context;
		imageLoader = new ImageLoader(context);
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
	}

	public View getView(final int position, final View convertView,
			final ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			row = inflater.inflate(R.layout.tweet_layout, null);
		}

		Tweet twit = (Tweet) getItem(position);
		String time = timeDuration(twit.getTime());
		final TextView timeDur = (TextView) row.findViewById(R.id.time);
		timeDur.setText(time);
		final TextView profileName = (TextView) row
				.findViewById(R.id.profileName);
		profileName.setText(twit.getFullName());
		final TextView username = (TextView) row.findViewById(R.id.userName);
		username.setText(" @" + twit.getUserName());
		ImageView image = (ImageView) row.findViewById(R.id.profileImage);

		imageLoader.DisplayImage(twit.getImageUrl(), image);

		final TextView twitMessage = (TextView) row
				.findViewById(R.id.twitMessage);
		twitMessage.setText(twit.getTweetText());
		int isRetweet = twit.getIsRetweet();
		final ImageView retweet_image = (ImageView) row
				.findViewById(R.id.image_retweet);
		final TextView retweet_user = (TextView) row
				.findViewById(R.id.name_user_retweet);

		if (isRetweet > 0) {
			if (twit.getNameRweet().length() > 0) {

				retweet_image.setVisibility(View.VISIBLE);
				retweet_user.setVisibility(View.VISIBLE);
				retweet_user.setText("  Retweeted by " + twit.getNameRweet());
			}
			else {
				retweet_image.setVisibility(View.GONE);
				retweet_user.setVisibility(View.GONE);
			}
			
		} else {
			retweet_image.setVisibility(View.GONE);
			retweet_user.setVisibility(View.GONE);
		}

		final ImageView conver_picture = (ImageView) row
				.findViewById(R.id.conver_picture);
		if (twit.getPost_url_images().length() > 0) {
			conver_picture.setVisibility(View.VISIBLE);
			conver_picture
					.setImageResource(R.drawable.ic_tweet_attr_photo_default);
		} else if (twit.getIsReply() > 0) {
			conver_picture.setVisibility(View.VISIBLE);
			conver_picture
					.setImageResource(R.drawable.ic_tweet_attr_convo_default);
		} else
			conver_picture.setVisibility(View.GONE);

		final TextView displayTime = (TextView) row.findViewById(R.id.time);

		String timeFormat = timeDuration(twit.getTime());

		displayTime.setText(timeFormat);

		final ImageView favorRetweet = (ImageView) row
				.findViewById(R.id.favor_retweet);
		if (twit.getIsMyFavorite() && twit.getIsMyRetweet()) {
			favorRetweet.setVisibility(View.VISIBLE);
			favorRetweet.setBackgroundResource(R.drawable.ic_dogear_both);
		} else if (twit.getIsMyFavorite()) {
			favorRetweet.setVisibility(View.VISIBLE);
			favorRetweet.setBackgroundResource(R.drawable.ic_dogear_fave);
		} else if (twit.getIsMyRetweet()) {
			favorRetweet.setVisibility(View.VISIBLE);
			favorRetweet.setBackgroundResource(R.drawable.ic_dogear_rt);
		} else
			favorRetweet.setVisibility(View.GONE);
		final LinearLayout context_menu = (LinearLayout) row
				.findViewById(R.id.context_menu);
		context_menu.setVisibility(View.GONE);

		return row;
	}

	public void appendItems(ArrayList items) {
		socialFeed.addAll(items);
		notifyDataSetChanged();
	}

	public void appendHeadItem(ArrayList items) {
		socialFeed.addAll(0, items);
		notifyDataSetChanged();
	}

	public int getCount() {
		return socialFeed.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */

	public Object getItem(final int index) {

		return socialFeed.get(index);
	}

	public void removeItem(int itemId) {
		int check = getItemById(itemId);
		if (check != -1) {
			socialFeed.remove(check);
			notifyDataSetChanged();
		}
	}

	public int getItemById(int id) {

		for (int i = 0; i < socialFeed.size(); i++) {
			Tweet tweet = socialFeed.get(i);
			if (tweet.getTweetId() == id)
				return i;
		}
		return -1;
	}

	public void favoriteById(Boolean favorite, int id) {
		int check = getItemById(id);
		if (check != -1) {
			Tweet tweet = socialFeed.get(check);
			tweet.setIsMyFavorite(favorite);
			// notifyDataSetChanged();
		}
	}

	public void retweetById(Boolean favorite, int id) {
		int check = getItemById(id);
		if (check != -1) {
			Tweet tweet = socialFeed.get(check);
			tweet.setIsMyRetweet(favorite);
			// notifyDataSetChanged();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */

	public long getItemId(final int index) {
		return index;
	}

	public void clearCache() {
		imageLoader.clearCache();
	}

	public static String timeDuration(String sDate) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		DateFormat dtFm = new SimpleDateFormat("dd MMM");
		Calendar server = new GregorianCalendar(
				TimeZone.getTimeZone("GMT+0:00"));
		server.setTimeInMillis(new Date().getTime());
		Calendar client = Calendar.getInstance();
		// .getTimeZone());
		client.setTimeInMillis(new Date().getTime());
		String result = "";
		try {
			int day = client.get(Calendar.DAY_OF_MONTH)
					- server.get(Calendar.DAY_OF_MONTH);
			int hour = client.get(Calendar.HOUR_OF_DAY)
					- server.get(Calendar.HOUR_OF_DAY);
			// String result = "";
			Date d1 = (Date) dateFormat.parse(sDate);
			String d2 = dtFm.format(d1);
			// Date d3 = (Date) dateFormat.parse(zone);
			long diff = System.currentTimeMillis() - d1.getTime()
					+ client.getTimeInMillis() - server.getTimeInMillis()
					- (day * 24 + hour) * 3600000;
			// System.out.println("Result"+diff);
			diff = diff / 1000;
			if (diff < 60) {
				result = "" + diff + "s";
				// System.out.print("Result"+result);
				// return result;
				// System.out.print("Result"+result);
			} else {
				diff = diff / 60;
				if (diff < 60) {
					result = "" + diff + "m";
					// System.out.print("Result"+result);
					// return result;
				} else {
					diff = diff / 60;
					if (diff < 24) {
						result = "" + diff + "h";
						// System.out.print("Result"+result);
						// return result;
					} else {
						// System.out.print("Result"+result);
						diff = diff / 24;
						if (diff < 7) {
							result = "" + diff + "d";
							// System.out.print("Result"+result);
							// return result;
						} else {
							result = "" + d2;
							// System.out.print("Result"+result);
							// return result;
						}
					}
				}
			}

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// System.out.print("Result"+result);
		return result;
	}
}
